package cn.text.ico;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileSystemView;

import org.apache.http.ParseException;

public class WelcomeInterface {
    public static JTextField path = new JTextField();
    public static JTextField number = new JTextField();
    public static JTextField page = new JTextField();
    public static JTextArea log = new JTextArea();
    public static JButton start = new JButton("开始下载");
    public static JButton stop = new JButton("强  制  关  闭");
    public static Thread thread;
    static int x = 0;

    public static void show() {
        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        jfc.setDialogTitle("选择文件目录保存文件 ");
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        if (jfc.showDialog(null, "选择") == JFileChooser.APPROVE_OPTION) {
            if (jfc.getSelectedFile().isDirectory()) {
                path.setText(jfc.getSelectedFile().getPath());
                x = 1;
            }
        } else if (x == 0) {
            JOptionPane.showMessageDialog(null, "您已退出！", "提示！", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }

    public static void welcome() {
        Text text = new Text();
        thread = new Thread(text);

        final JFrame jfindex = new JFrame();
        jfindex.setTitle("ICO图标下载指引");
        jfindex.setBounds(30, 30, 1080, 700);
        final JLabel dJLabel = new JLabel();
        dJLabel.setFont(new Font("微软雅黑", Font.BOLD, 40));
        dJLabel.setText("不老实o(╥﹏╥)o(^_−)☆∑(っ°Д°;)っ卧槽，不见了");
        dJLabel.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 1000) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - 50) / 2, 1000, 50);
        jfindex.add(dJLabel);
        dJLabel.setVisible(false);
        final Jfindex fJfindex = new Jfindex();
        jfindex.add(fJfindex);
        jfindex.setVisible(true);
        jfindex.setAutoRequestFocus(true);

        jfindex.addWindowStateListener(new WindowStateListener() {
            @Override
            public void windowStateChanged(WindowEvent arg0) {
                if (arg0.getNewState() == 6) {
                    fJfindex.setVisible(false);
                    dJLabel.setVisible(true);
                } else if (arg0.getNewState() == 0) {
                    dJLabel.setVisible(false);
                    fJfindex.setVisible(true);
                }
            }
        });
        JFrame jF = new JFrame();
        JMenuBar popup = new JMenuBar();
        JMenuItem item = new JMenuItem("                                      重新选择保存路径");
        popup.add(item);
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                show();
            }
        });
        jF.setJMenuBar(popup);
        jF.setTitle("Logo神器Ico图片爬取软件");
        jF.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 350) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - 500) / 2, 350, 500);
        jF.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        path.setBounds(130, 45, 200, 20);
        path.setEditable(false);
        jF.add(path);

        JLabel path = new JLabel();
        path.setText("保存的路径：");
        path.setBounds(20, 45, 120, 20);
        path.setFont(new Font("楷体", Font.LAYOUT_NO_LIMIT_CONTEXT, 20));
        jF.add(path);

        number.setBounds(120, 90, 100, 20);
        jF.add(number, BorderLayout.CENTER);

        JLabel path2 = new JLabel();
        path2.setText("logo索引：");
        path2.setFont(new Font("楷体", Font.LAYOUT_NO_LIMIT_CONTEXT, 20));
        path2.setBounds(25, 90, 150, 20);
        jF.add(path2);

        page.setBounds(140, 130, 30, 20);
        page.setDocument(new NumberLenghtLimitedDmt(3));
        jF.add(page, BorderLayout.CENTER);

        JLabel path3 = new JLabel();
        path3.setText("下载多少页：  （一页20张logo）");
        path3.setFont(new Font("楷体", Font.LAYOUT_NO_LIMIT_CONTEXT, 20));
        path3.setBounds(25, 130, 300, 20);
        jF.add(path3);

        log.setLineWrap(true);
        log.setText("下载日志：\n\r");
        JScrollPane jScrollPane = new JScrollPane(log);
        jScrollPane.setBounds(25, 160, 290, 250);
        log.setEditable(false);
        jF.add(jScrollPane);

        JLabel ti = new JLabel();
        ti.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));
        ti.setText("欢 迎 使 用 LOGO 网 爬 虫 软 件");
        ti.setBounds(13, 10, 370, 20);
        jF.add(ti);
        start.setBounds(230, 75, 100, 40);
        jF.add(start);
        stop.setBounds(10, 415, 330, 30);
        jF.add(stop);
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (number.getText().length() <= 0) {//
                    JOptionPane.showMessageDialog(null, "请输入选择的序号！", "提示！", JOptionPane.OK_CANCEL_OPTION);
                } else if (page.getText().length() <= 0) {
                    JOptionPane.showMessageDialog(null, "请输入下载的总页数！", "提示！", JOptionPane.OK_CANCEL_OPTION);
                } else if (number.getText().length() <= 0) {
                    JOptionPane.showMessageDialog(null, "请输入正确的下载类型！", "错误！", JOptionPane.ERROR_MESSAGE);
                } else if (Integer.parseInt(page.getText()) == 0) {
                    JOptionPane.showMessageDialog(null, "请输入正确的页数！", "错误！", JOptionPane.ERROR_MESSAGE);
                } else {
                    try {
                        start.setSelected(false);
                        number.setEditable(false);
                        page.setEditable(false);
                        JOptionPane.showMessageDialog(null, "下载logo数量根据网页拥有数定！", "提示！",
                                JOptionPane.INFORMATION_MESSAGE);
                        thread.start();
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                        JOptionPane.showMessageDialog(null, "网络连接异常（404）！", "错误！", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        stop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if (JOptionPane.showConfirmDialog(null, "确定强制退出？", "提示！",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });
        jF.setResizable(false);
        jF.setLayout(null);
        jF.setVisible(true);

    }
}
